<template>
  <div class="product">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>账户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-button
        type="primary"
        size="mini"
        class="insbut"
        icon="el-icon-circle-plus-outline"
        @click="dialogFormVisible = true"
        >新增管理员</el-button
      >
      <div class="conter">
        <el-table :data="tableData" border stripe>
          <el-table-column
            type="index"
            :index="indexNum"
            label="序号"
            width="60"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="id" label="管理员ID" width="80" align="center">
          </el-table-column>
          <el-table-column
            prop="loginname"
            label="登录账号"
            width="80"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="loginpassword"
            label="登录密码"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column prop="nickname" label="昵称" width="" align="center">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="联系方式"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column prop="email" label="邮箱" width="" align="center">
          </el-table-column>
          <el-table-column prop="sex" label="性别" width="" align="center">
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="创建时间"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="updatetime"
            label="修改时间"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column label="禁用/启用" width="140" align="center">
            <template slot-scope="scpoe">
              <el-switch
                v-model="scpoe.row.state"
                active-text="启用"
                inactive-text="禁用"
                @change="toggle(scpoe.row.id)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="Editadmin(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="Deleteadmin(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加产品弹框 对话框-->
      <el-dialog
        :title="isEdit ? '编辑' : '新增管理员'"
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="form">
          <el-form-item
            v-if="isEdit"
            label="管理员Id"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.id"
              autocomplete="off"
              readonly
              title="不可修改"
            ></el-input>
          </el-form-item>
          <el-form-item label="登录账号" :label-width="formLabelWidth">
            <el-input v-model="form.loginname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="登录密码" :label-width="formLabelWidth">
            <el-input
              v-model="form.loginpassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="联系方式" :label-width="formLabelWidth">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-input v-model="form.sex" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="isEdit" type="primary" @click="Updateadmin"
            >提 交</el-button
          >
          <el-button v-else type="primary" @click="Insertadmin"
            >新 增</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  
  data() {
    return {
      tableData: [],
      form: {
        loginname: "",
        loginpassword: "",
        nickname: "",
        phone: "",
        email: "",
        sex: ""
      },
      formLabelWidth: "80px",
      dialogFormVisible: false,
      input: "",
      isEdit: false
    };
  },
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    // 提交图片和数据去服务器 插入数据
    Insertadmin() {
      const that = this;
      that.$http
        .post("/insertadmin", that.form)
        .then(res => {
          // console.log(res);
          if (res.status == 204) {
            that.$message({
              message: "上传成功...",
              type: "success"
            });
            that.init();
          } else {
            that.$message({
              message: "上传失败...",
              type: "error"
            });
          }
          console.log(res);
          that.dialogFormVisible = false;
          that.form.loginname = null;
          that.form.loginpassword = null;
          that.form.nickname = null;
          that.form.phone = null;
          that.form.email = null;
          that.form.sex = null;
        })
        .catch(function() {});
    },
    //请求后端数据
    init() {
      const that = this;
      this.$http
        .get("/selectadmin")
        .then(res => {
          // console.log(res);
          that.tableData = res.data;
          for (let i in that.tableData) {
            if (that.tableData[i].state == 1) {
              that.tableData[i].state = true;
            } else {
              that.tableData[i].state = false;
            }
          }
        })
        .catch(function() {
          alert("请求数据失败...");
        });
    },
    //element table自带序号
    indexNum(index) {
      return index + 1;
    },
    // switch开关改变 触发的回调函数 下架或上架
    toggle(id) {
      //   console.log(id);
      this.$http.post("/adminswitch", { id });
    },
    //编辑图书 返回数据到表单
    Editadmin(item) {
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(item));
    },
    //更新编辑好的管理员
    Updateadmin() {
      const that = this;
      that.$http
        .post("/updateadmin", that.form)
        .then(res => {
          if (res.status == 204) {
            that.$message({
              message: "更新数据成功...",
              type: "success"
            });
            that.init();
            that.dialogFormVisible = false;
            that.isEdit = false;
          } else {
            that.$message({
              message: "更新数据失败...",
              type: "error"
            });
          }
          // console.log(res);
          that.dialogFormVisible = false;
          that.form.loginname = null;
          that.form.loginpassword = null;
          that.form.nickname = null;
          that.form.phone = null;
          that.form.email = null;
          that.form.sex = null;
        })
        .catch(function() {});
    },
    //删除管理员
    Deleteadmin(id) {
      const that = this;
      that
        .$confirm("此操作将数据永久删除,不可恢复, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        })
        .then(() => {
          that.$http.post("/deleteadmin", { id }),
            that.$message({
              type: "success",
              message: "删除成功!"
            });
          for (let i in that.tableData) {
            if (that.tableData[i].id == id) {
              that.tableData.splice(i, 1); //删除不请求数据，直接在数组中删除
              break;
            }
          }
        })
        .catch(() => {
          that.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>